<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Redis\Channel;

/**
 * Class ManyiController
 * @package Pay\Controller
 * Mana支付
 */
class ManaController extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $ismobile = isMobile();
        $data = array();
        $data['fxid'] = $this->config['mch_id'];
        $data['fxddh'] = $return['orderid'];
        $data['fxdesc'] = 'zf';
        $data['fxfee'] = $return['amount'];
        $data['fxattch'] = 'zf';
        $data['fxnotifyurl'] = $return['notifyurl'];
        $data['fxbackurl'] = $return['callbackurl'];

        switch($this->config['paytype']){
            //微信
            case 1:
                $data['fxpay'] = 'wxsm';
                break;
            //微信H5
            case 2:
                $data['fxpay'] = 'wxpay';
                break;
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:
                if($ismobile){
                    $data['fxpay'] = 'zfbsmsss';
                }else{
                    $data['fxpay'] = 'zfbsmsss';
                }
                break;
            case 9:
            case 11:
            case 19:
                $data['fxpay'] = 'ylewm';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);

        }
        $data['fxip'] = $_SERVER['REMOTE_ADDR'];
        $data["fxsign"] = md5($data["fxid"] . $data["fxddh"] . $data["fxfee"] . $data["fxnotifyurl"] . $this->config['signkey']);
        $res = curl($this->config['gateway'], arrToUrlStr($data));

        if(!$res){
            $this->showmessage('未获取到数据!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        $res = json_decode($res, true);
        if(empty($res)){
            $this->showmessage('获取的数据类型错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        //echo '<pre/>'; print_r($data); print_r($res); exit;

        $code = $res['payurl'];
        if(!$code){
            if($res['error']){
                $this->showmessage($res['error'],['code'=>C("THIRDCODES.".$this->class)]);
            }
            $this->showmessage("获取支付链接失败",['code'=>C("THIRDCODES.".$this->class)]);
        }

        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("wap_url", $code);
        if(!$ismobile){
            $this->assign("code_url", $code);
        }else{
            $this->assign("code_url", $code);
            //$this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=" . base64_encode($code));
        }
        $this->assign("deny_layer",1);
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);

        switch($this->config['paytype']){
            //微信
            case 1:
                //header("Location:{$code}"); exit;
                if($ismobile){
                    $this->assign("code_url", $code);
                    $this->display("WeiXin/weixinwap");
                }else{
                    $this->assign("code_url", $code);
                    $this->display("WeiXin/weixin");
                }
                break;
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:
                //$this->assign("code_url", $code);
                $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=" . base64_encode($code));
                
                if($ismobile){
                    $this->display("WeiXin/alipaywap");
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            case 9:
            case 11:
            case 19:
                header("Location:{$code}");
                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    //通知
    public function notifyurl(){
        //$this->debug_callback();
        if(!$this->checkip(getIP(),['103.105.58.249'])){
            exit("wrong");
        }
        $p = I("post.");

        /*$p = array (
            'fxid' => '2018101',
            'fxddh' => '201808081340147836194',
            'fxorder' => '2018080839146767',
            'fxdesc' => 'zf',
            'fxfee' => '30.00',
            'fxattch' => 'zf',
            'fxstatus' => '1',
            'fxtime' => '1533706863',
            'fxsign' => '3a645a14d052a3b26ad5503a79be97c5',
        );*/

        if(empty($p)){
            exit("");
        }

        //获取订单信息
        $orderid = $p['fxddh'];
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "pay_orderid_rd"=> htmlspecialchars($p['fxorder']),
            "pay_true" => floatval($p['fxfee']),
        ));

        if(abs($find['pay_amount'] - $p['fxfee']) > 1){
            exit("illegal");
        }

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        if($p['fxstatus'] !== '1'){
            exit("fail");
        }

        $sign = $p['fxsign'];

        //md5(订单状态+商务号+商户订单号+支付金额+商户秘钥
        $str = $p['fxstatus'] . $p['fxid'] . $p['fxddh'] . $p['fxfee'] . $this->config['signkey'];
        $sign2 = md5($str);

        if($sign !== $sign2){
            echo 'sign_wrong';exit;
        }

        $this->async($find['pay_orderid'],'success');
    }

    public function callBackUrl(){

        echo '操作成功'; exit;
    }
}